//
//  ServiceInfoVC.m
//  UHealth
//
//  Created by hanyou on 15/3/23.
//  Copyright (c) 2015年 gjtx. All rights reserved.
//

#import "ServiceInfoVC.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "iToast.h"
#import "MJRefresh.h"
#import "SubmitOrderVC.h"



@interface ServiceInfoVC ()

@property (nonatomic,strong) UIImageView    *img;
@property (nonatomic,strong) UILabel        *serL;
@property (nonatomic,strong) UILabel        *detL;
@property (nonatomic,strong) UILabel        *gysL;
@property (nonatomic,strong) UIWebView      *webV;
@property (nonatomic,strong) id             webRes;

@property (nonatomic,strong) NSDictionary   *serviceDic;

@end

@implementation ServiceInfoVC

- (id)initWithDic:(NSDictionary *)dic
{
    _serviceDic = dic;
    self = [super init];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = kBacColor;
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:kLocal(@"serviceInfo")];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:kLocal(@"serviceInfo")];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NavView *nav = [[NavView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, kNavHeight) andTitleStr:kLocal(@"serviceInfo") andVC:self];
    [self.view addSubview:nav];
    
    UIScrollView *scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kNavHeight, WIDTH, HEIGHT - kNavHeight)];
    [self.view addSubview:scrollV];
    
    _img = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, WIDTH -30, 100)];
    [_img sd_setImageWithURL:[NSURL URLWithString:_serviceDic[@"appfile"]] placeholderImage:[UIImage imageNamed:@"pic2"]];
    [scrollV addSubview:_img];
    
    _serL = [[UILabel alloc]initWithFrame:CGRectMake(15, 120, WIDTH-30, 20)];
    _serL.text = _serviceDic[@"shop_name"];
    [scrollV addSubview:_serL];
    
    _detL = [[UILabel alloc]init];
    _detL.numberOfLines = 0;
    _detL.font = [UIFont systemFontOfSize:14];
    _detL.lineBreakMode = NSLineBreakByWordWrapping;
    _detL.text = _serviceDic[@"js"];
    CGSize size =[_detL.text boundingRectWithSize:CGSizeMake(WIDTH,2000) options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_detL.font} context:nil].size;
    _detL.frame =  CGRectMake(15,150, WIDTH-30, size.height);
    [scrollV addSubview:_detL];
    
    _gysL = [[UILabel alloc]initWithFrame:CGRectMake(15, 161+_detL.frame.size.height, WIDTH-30, 20)];
    _gysL.text = _serviceDic[@"gys"];
    [scrollV addSubview:_gysL];
    
    UIButton *xsBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    xsBtn.frame =CGRectMake(15, 190+_detL.frame.size.height, WIDTH/2 -30, 30);
    [xsBtn setTitle:kLocal(@"service_detail") forState:UIControlStateNormal];
    xsBtn.tag = 1;
    [xsBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [scrollV addSubview:xsBtn];
    
    UIButton *fwBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    fwBtn.frame =CGRectMake(WIDTH/2 + 15, 190+_detL.frame.size.height, WIDTH/2 -30, 30);
    [fwBtn setTitle:kLocal(@"serviceInfo") forState:UIControlStateNormal];
    fwBtn.tag = 2;
    [fwBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [scrollV addSubview:fwBtn];
    
    _webV = [[UIWebView alloc]initWithFrame:CGRectMake(15, 220+_detL.frame.size.height, WIDTH-30, 200)];
    _webV.scrollView.bounces = NO;
    [scrollV addSubview:_webV];
    
    scrollV.showsVerticalScrollIndicator = NO;
    float hei = 420+_detL.frame.size.height>HEIGHT+10? 420+_detL.frame.size.height:HEIGHT+10;
    scrollV.contentSize = CGSizeMake(WIDTH, hei);
    
    if(kNetID){
        UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        buyBtn.frame = CGRectMake(15, 440+_detL.frame.size.height, WIDTH-30, 40);
        [buyBtn.layer setMasksToBounds:YES];
        [buyBtn.layer setCornerRadius:1.0];//设置矩形是个圆角半径
        buyBtn.backgroundColor = kBlueColor;
        [buyBtn setTitle:kLocal(@"buyService") forState:UIControlStateNormal];
        [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [buyBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        buyBtn.tag = 3;
        [scrollV addSubview:buyBtn];
        float hei = _detL.frame.size.height +480>HEIGHT+10? _detL.frame.size.height +480:HEIGHT+10;
        scrollV.contentSize = CGSizeMake(WIDTH, hei);
    }
    
    
    __unsafe_unretained typeof(self) vc = self;
    __unsafe_unretained typeof(scrollV) sc = scrollV;
    
    [scrollV addHeaderWithCallback:^{
        
        if(!kNetIsOK){
            [sc headerEndRefreshing];
            [[iToast makeText:kLocal(@"netNoOk")] show];
            return ;
        }
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer.timeoutInterval = 5;
        manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
        NSString *postStr = [NSString stringWithFormat:@"%@/action/json/shop.jsp?",kBaseUrl];
        NSDictionary *parameters = @{@"action": @"getshopdetail",
                                     @"userid":[NSString stringWithFormat:@"%d",(int)kNetID],
                                     @"shopid":_serviceDic[@"id"],};
        
        [manager POST:postStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            kDebug(@"Success: %@", operation.responseString);
            BOOL type = [[responseObject objectForKey:@"type"] boolValue];
            if(type){//成功
                vc.webRes = responseObject;
            }else{
                
            }
            //            [[iToast makeText:[responseObject objectForKey:@"msg"]] show];
            [sc headerEndRefreshing];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            kDebug(@"Error: %@", error);
            [sc headerEndRefreshing];
            [[iToast makeText:kLocal(@"netError")] show];
        }];
        
    }];
    [scrollV headerBeginRefreshing];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [_webV loadHTMLString:[_webRes objectForKey:@"xiangshou"] baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];
    
}

-(void)btnClick:(UIButton *)btn
{
    
    if (btn.tag == 1) {
        [_webV loadHTMLString:[_webRes objectForKey:@"xiangshou"] baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];
    }else if(btn.tag == 2){
        [_webV loadHTMLString:[_webRes objectForKey:@"fuwu"] baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]  bundlePath]]];
    }else if (btn.tag == 3) {
        if (kNetID) {
            SubmitOrderVC *sub = [[SubmitOrderVC alloc]initWithDic:_serviceDic];
            [self presentViewController:sub animated:YES completion:nil];
        }else{
            AppDelegate *de = (AppDelegate*)[UIApplication sharedApplication].delegate;
            [de changRootVCToLaunch];
        }
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
